home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / parsed / parse.bas < prev    next >
BASIC Source File  |  1995-05-09  |  7KB  |  230 lines

  1. Option Explicit
  2.  
  3. 'form show
  4. Global Const MODAL = 1
  5.  
  6. 'msgbox
  7. Global Const MB_ICONINFORMATION = 64
  8.  
  9. 'mousePointer
  10. Global Const DEFAULT = 0
  11. Global Const HOURGLASS = 11
  12.  
  13. 'WindowState
  14. Global Const NORMAL = 0    ' 0 - Normal
  15. Global Const MINIMIZED = 1 ' 1 - Minimized
  16. Global Const MAXIMIZED = 2 ' 2 - Maximized
  17.  
  18. 'clipboard object
  19. Global Const CF_TEXT = 1
  20.  
  21. Sub EndProg ()
  22.    Dim c%
  23.  
  24.    For c% = Forms.Count - 1 To 0 Step -1
  25.       Unload Forms(c%)
  26.    Next c%
  27.    End
  28.  
  29. End Sub
  30.  
  31. 'load selected text file and return the contents
  32. 'in a string variable
  33. Function LoadFile$ (TheFile$, FileLength&)
  34.    'string var containing file text
  35.    Dim FileStr$
  36.    Dim FNum%
  37.  
  38.    FNum% = FreeFile
  39.  
  40.    Open TheFile$ For Input As #FNum
  41.    
  42.    FileStr$ = Input$(FileLength&, #FNum)
  43.  
  44.    Close #FNum
  45.  
  46.    LoadFile$ = FileStr$
  47.  
  48. End Function
  49.  
  50. 'Program startup
  51. Sub Main ()
  52.    ChDir App.Path
  53.    frmParse.Show
  54. End Sub
  55.  
  56. 'Routine to accept a string to work on and a delimiter,
  57. 'and fill the passed dynamic array with parsed data.
  58. '
  59. 'Parameters:
  60. '
  61. 'TheString$ - String to parse
  62. 'Delim$ - Delimiting string (may be single or multiple characters)
  63. 'DynArray$() - An 'unredimmed' dynamic array passed by reference
  64. '(VB's default) which is filled with the parsed data which can be
  65. 'accessed by the calling routine.
  66. '
  67. 'Return Value: Number of elements.  See NOTE: below.
  68. '
  69. Function ParseAndFillArray1% (TheString$, Delim$, DynArray$())
  70.    Dim ElementCt%, CurPos%, LenAssigned%, CurStrLen%, LenDelim%
  71.    
  72.    LenDelim% = Len(Delim$)
  73.    'initialize array element count
  74.    ElementCt% = 1
  75.    'starting instr pos
  76.    CurPos% = 1
  77.    LenAssigned% = 1
  78.    
  79.    CurStrLen% = Len(TheString$)
  80.  
  81.    Do
  82.       'allocate the array element
  83.       ReDim Preserve DynArray$(1 To ElementCt%)
  84.       'get the current segment (not including delimiter)
  85.       CurStrLen% = (InStr(CurPos%, TheString$, Delim$) - CurPos%)
  86.       If CurStrLen% < 0 Then
  87.       'if delimiter not found
  88.      'we have the last segment
  89.      'assign the value to array (the last, righthand part)
  90.      DynArray$(ElementCt%) = Right$(TheString$, (Len(TheString$) - (LenAssigned% - 1)))
  91.      'done
  92.      Exit Do
  93.       Else
  94.      'assign the value to array
  95.       DynArray$(ElementCt%) = Mid$(TheString$, CurPos%, CurStrLen%)
  96.       End If
  97.       'assign start position of next element
  98.       'add length of current string to length assigned var.
  99.       LenAssigned% = LenAssigned% + (Len(DynArray$(ElementCt%)) + LenDelim%)
  100.       'set the new starting position (ahead of current delimiter) for next extraction
  101.       CurPos% = LenAssigned%
  102.       'increment array index for next element
  103.       ElementCt% = ElementCt% + 1
  104.    Loop
  105.  
  106.    'return # elements - NOTE: If the passed string ends in the delimiter,
  107.    'the last element in the array will be the empty string
  108.    'If this is the case, subtract 1 from the number returned
  109.    If DynArray$(ElementCt%) = "" Then
  110.       ParseAndFillArray1% = ElementCt% - 1
  111.    Else
  112.       ParseAndFillArray1% = ElementCt%
  113.       'or ParseAndFillArray1% = UBound(DynArray$)
  114.    End If
  115. End Function
  116.  
  117. 'Routine to accept a string to work on and a delimiter,
  118. 'and fill the passed dynamic array with parsed data.
  119. '
  120. 'Parameters:
  121. '
  122. 'TheString$ - String to parse
  123. 'Delim$ - Delimiting string (may be single or multiple characters)
  124. 'DynArray$() - An 'unredimmed' dynamic array passed by reference
  125. '(VB's default) which is filled with the parsed data which can be
  126. 'accessed by the calling routine.
  127. 'SetReDim% - This is a parameter which sets the frequency of array
  128. 'resizing so performance can be optimized. This parm. is the only
  129. 'difference between this routine and ParseAndFillArray1%
  130. '
  131. '
  132. 'Return Value: Number of elements.  See NOTE: below.
  133. '
  134. '
  135. Function ParseAndFillArray2% (TheString$, Delim$, DynArray$(), SetReDim%)
  136.    Dim ElementCt%, CurPos%, LenAssigned%, CurStrLen%, LenDelim%
  137.    
  138.    LenDelim% = Len(Delim$)
  139.    'initialize array element count
  140.    ElementCt% = 1
  141.    'starting instr pos
  142.    CurPos% = 1
  143.    LenAssigned% = 1
  144.    
  145.    CurStrLen% = Len(TheString$)
  146.  
  147.    'do initial array alloc.
  148.    ReDim DynArray$(1 To SetReDim%)
  149.  
  150.    Do
  151.       If ElementCt% Mod SetReDim% = 0 Then
  152.      ReDim Preserve DynArray$(1 To ElementCt% + SetReDim%)
  153.       End If
  154.       'get the current segment (not including delimiter)
  155.       CurStrLen% = (InStr(CurPos%, TheString$, Delim$) - CurPos%)
  156.       If CurStrLen% < 0 Then
  157.       'if delimiter not found
  158.      'we have the last segment
  159.      'assign the value to array (the last, righthand part)
  160.      DynArray$(ElementCt%) = Right$(TheString$, (Len(TheString$) - (LenAssigned% - 1)))
  161.      'done
  162.      Exit Do
  163.       Else
  164.      'assign the value to array
  165.       DynArray$(ElementCt%) = Mid$(TheString$, CurPos%, CurStrLen%)
  166.       End If
  167.       'assign start position of next element
  168.       'add length of current string to length assigned var.
  169.       LenAssigned% = LenAssigned% + (Len(DynArray$(ElementCt%)) + LenDelim%)
  170.       'set the new starting position (ahead of current delimiter) for next extraction
  171.       CurPos% = LenAssigned%
  172.       'increment array index for next element
  173.       ElementCt% = ElementCt% + 1
  174.    Loop
  175.  
  176.    'return # elements - NOTE: If the passed string ends in the delimiter,
  177.    'the last element in the array will be the empty string
  178.    'If this is the case, subtract 1 from the number returned
  179.    If DynArray$(ElementCt%) = "" Then
  180.       ParseAndFillArray2% = ElementCt% - 1
  181.    Else
  182.       ParseAndFillArray2% = ElementCt%
  183.    End If
  184. End Function
  185.  
  186. 'Function to process a passed string array and return
  187. 'a new string consisting of the passed array elements
  188. 'concatenated with a separator. Used in conjunction with
  189. 'ParseAndFillArray%(). If an element is an empty string
  190. '(this will occur when there are two or more consecutive
  191. 'crlf's in the text), discard the crlf and do not add another space.
  192. '
  193. 'Parameters:
  194. '
  195. 'DynArray$() - An 'unredimmed' dynamic array passed by reference
  196. '(VB's default) which is filled with the processed data which can be
  197. 'accessed by the calling routine.
  198. 'separator$ - Character to put between elements in creating the
  199. 'build string.
  200. 'AdjustedLineCt% - A value passed by reference which will return
  201. 'a line count value which does not consider single CRLF's a line.
  202. '
  203. 'Return Value: The string constructed from the array.
  204. '
  205. Function ProcessArray$ (DynArray$(), Separator$, AdjustedLineCt%)
  206.    Dim BuildStr$
  207.    Dim i%
  208.  
  209.    'go through the array (to the second to last element)
  210.    'adding each element to the build string plus the
  211.    'separator. Elements consisting of an empty string are
  212.    'bypassed.
  213.    For i% = 1 To UBound(DynArray$) - 1
  214.       If DynArray(i%) <> "" Then
  215.      BuildStr$ = BuildStr$ & DynArray(i%) & Separator$
  216.      'increment adjusted linecount
  217.      AdjustedLineCt% = AdjustedLineCt% + 1
  218.       End If
  219.    Next i%
  220.    
  221.    'add the last element - do not append separator
  222.    BuildStr$ = BuildStr$ & DynArray$(UBound(DynArray$))
  223.    'increment adjusted linecount
  224.    AdjustedLineCt% = AdjustedLineCt% + 1
  225.    
  226.    'return the built string
  227.    ProcessArray$ = BuildStr$
  228. End Function
  229.  
  230.